const { build } = require("esbuild");
const path = require("path");
const args = require("minimist")(process.argv.slice(2)); //这个是获取命令行参数的模块
//这个是打包的模块是哪一个
const target = args._[0] || "reactivity";
const format = args.f || "global";
// console.log(args);
const pkg = require(path.resolve(
  __dirname,
  "../packages/" + target + "/package.json"
));
const outputFormat = format.startsWith("global")
  ? "iife"
  : format === "cjs"
  ? "cjs"
  : "esm";
// cjs ==>node 打包 esm ==>浏览器 iife==>直接引入(立即执行函数)

//打包输出文件
//reactivity/dist/reactivity.global.js
//reactivity/dist/reactivity.esm.js
//reactivity/dist/reactivity.cjs.js
const outfile=path.resolve(__dirname,`../packages/${target}/dist/${target}.${format}.js`)
// console.log(outputFormat,outfile,path.resolve(__dirname, "../packages/" + target + "/src/index.ts"));
build({
  entryPoints: [path.resolve(__dirname, "../packages/" + target + "/src/index.ts")],
  outfile,
  bundle: true,
  sourcemap: true,
  format: outputFormat,
  globalName: pkg.buildOptions?.name,
  platform:format === "cjs" ? "node" : "browser", //
}).then((res)=>{
  // console.log('build watch')
  console.log(res);
})